using System;
namespace ch.ethz.ssh2.crypto.cipher
{
	
	/*
	This file was shamelessly taken (and modified) from the Bouncy Castle Crypto package.
	Their licence file states the following:
	
	Copyright (c) 2000 - 2004 The Legion Of The Bouncy Castle
	(http://www.bouncycastle.org)
	
	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:
	
	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.
	
	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE. 
	*/
	
	/// <summary> DESede.
	/// 
	/// </summary>
	/// <author>  See comments in the source file
	/// </author>
	/// <version>  2.50, 03/15/10
	/// 
	/// </version>
	public class DESede:DES
	{
		override public System.String AlgorithmName
		{
			get
			{
				return "DESede";
			}
			
		}
		override public int BlockSize
		{
			get
			{
				return 8;
			}
			
		}
		private int[] key1 = null;
		private int[] key2 = null;
		private int[] key3 = null;
		
		private bool encrypt;
		
		/// <summary> standard constructor.</summary>
		public DESede()
		{
		}
		
		/// <summary> initialise a DES cipher.
		/// 
		/// </summary>
		/// <param name="encrypting">whether or not we are for encryption.
		/// </param>
		/// <param name="key">the parameters required to set up the cipher.
		/// </param>
		/// <exception cref="IllegalArgumentException">if the params argument is inappropriate.
		/// </exception>
		public override void  init(bool encrypting, byte[] key)
		{
			key1 = generateWorkingKey(encrypting, key, 0);
			key2 = generateWorkingKey(!encrypting, key, 8);
			key3 = generateWorkingKey(encrypting, key, 16);
			
			encrypt = encrypting;
		}
		
		public override void  transformBlock(byte[] input, int inOff, byte[] output, int outOff)
		{
			if (key1 == null)
			{
				throw new System.SystemException("DESede engine not initialised!");
			}
			
			if (encrypt)
			{
				desFunc(key1, input, inOff, output, outOff);
				desFunc(key2, output, outOff, output, outOff);
				desFunc(key3, output, outOff, output, outOff);
			}
			else
			{
				desFunc(key3, input, inOff, output, outOff);
				desFunc(key2, output, outOff, output, outOff);
				desFunc(key1, output, outOff, output, outOff);
			}
		}
		
		public override void  reset()
		{
		}
	}
}